<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>店长推荐编辑</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/layuimini.css?v=2.0.4.2}" media="all">
    <link rel="stylesheet" th:href="@{/css/themes/default.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>

        body{
            padding: 20px;
        }


    </style>
</head>
<body>
<form class="layui-form" action="" th:object="${suit}">
    <input type="hidden" th:field="*{id}">
    <div class="layui-form-item">
        <label class="layui-form-label">整机</label>
        <img id="demo1" th:src="*{'/upload/' + img}" width="200px">
        <button type="button" class="layui-btn" id="imageUpload">
            <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">配置单</label>
        <div class="layui-input-block">

            <input type="text" th:field="*{sheet}"  required="required"  lay-verify="required" placeholder="请输入厂家名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">优惠</label>
        <div class="layui-input-block">
            <input type="text" th:field="*{discount}" required="required"  lay-verify="required" placeholder="请输入优惠力度" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">游戏性能</label>
        <div class="layui-input-block">

            <input type="text" th:field="*{game}"  required="required"  lay-verify="required" placeholder="请输入游戏性能" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">办公性能</label>
        <div class="layui-input-block">

            <input type="text" th:field="*{work}"  required="required"  lay-verify="required" placeholder="请输入办公性能" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">

            <input type="text" th:field="*{price}"  required="required"  lay-verify="required" placeholder="请输入价格" autocomplete="off" class="layui-input">
        </div>
    </div>



    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea th:field="*{record}" placeholder="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="btn-submit" class="layui-btn" lay-submit lay-filter="btn-submit-filter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script th:inline="javascript">
    var ctxPath = [[@{/}]];

        layui.use(['laydate','table','form', 'upload'], function(){
            var laydate = layui.laydate;
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;

            var fileUrl = '[[*{img}]]'
            console.debug('我去', fileUrl)
            var upload = layui.upload;  // 调用工具
            upload.render({
                elem: "#imageUpload",  // 指向容器选择器，与id进行绑定
                url: ctxPath + "suit/upload",
                before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                },
                done: function (res) {
                    if (res.code == 0) {
                        layer.msg('图片上传成功')
                        fileUrl = res.data.file
                    } else {
                        layer.msg('图片上参失败')}
                }
            })

            //表单提交

            form.on('submit(btn-submit-filter)', function(data){
                // 将登录按钮置灰
                $("#btn-submit").attr("disabled","disabled").addClass("layui-btn-disabled");

                // 异步请求提交数据

                var url = ctxPath + "suit";
                data.field._method = "PUT";
                var reqData = Object.assign(data.field, { img: fileUrl })
                $.post(url,reqData,function (response){

                    if (response.code == 0){

                        //成功提示
                        layer.msg(response.message, {
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });

                        //关闭窗口

                        //刷新表格
                    }else{
                        $("#btn-submit").removeAttr("disabled","disabled").removeClass("layui-btn-disabled");

                        //失败

                        layer.alert(response.message, {
                            icon: 2,
                            anim: 6,
                            title: "编辑失败"

                        });
                        // 将登录按钮取消置灰,恢复可用


                    }






                });



                // 提交成功后则关闭窗口，并且刷新数据表格
                //失败则提示错误信息，窗口不关闭

                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });




        });
</script>



</body>
</html>